package com.qf.service.serviceImpl;

import com.qf.dao.UserDao;
import com.qf.dao.daoImpl.UserDaoImpl;
import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.util.Constants;
import com.qf.util.MD5Utils;

public class UserServiceImpl implements UserService {
    UserDao userDao =  new UserDaoImpl();

    @Override
    public User login(String username, String password) {
        User user = userDao.FindUserByName(username);
        if (user == null){
            return  null;
        }
     //   判断是否激活
        if (!Constants.USER_ACTIVE.equals(user.getStatus())) {
            return null;
        }

        String md5Pwd = MD5Utils.md5(password);
        if (md5Pwd.equals(user.getPassword())){
            return  user;
        }
        return null;
    }

    @Override
    public boolean check(String username) {
        User user = userDao.FindUserByName(username);
        if (user == null){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public int createUser(User user) {
        String md5 = MD5Utils.md5(user.getPassword());
        user.setPassword(md5);
        user.setStatus(Constants.USER_ACTIVE);
        int count = userDao.addUser(user);
        return count;
    }
}
